package com.dev.bbs.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dev.bbs.bean.BbsSignInfo;
import com.dev.bbs.bean.RankSign;
import com.dev.bbs.domain.BbsUser;
import com.dev.bbs.domain.BbsUserSign;
import com.dev.bbs.mapper.BbsUserSignMapper;
import com.dev.bbs.service.IBbsSignRuleService;
import com.dev.bbs.service.IBbsUserService;
import com.dev.bbs.service.IBbsUserSignService;
import com.dev.common.utils.DateUtils;

/**
 * 会员签到Service业务层处理
 * 
 * @author dev
 * @date 2019-09-19
 */
@Service
public class BbsUserSignServiceImpl implements IBbsUserSignService
{
    @Autowired
    private BbsUserSignMapper   bbsUserSignMapper;

    @Autowired
    private IBbsSignRuleService signRuleService;

    @Autowired
    private IBbsUserService     userService;

    /**
     * 查询会员签到
     * 
     * @param userSigninId 会员签到ID
     * @return 会员签到
     */
    @Override
    public BbsUserSign selectBbsUserSigninById(Long id)
    {
        return bbsUserSignMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询会员签到列表
     * 
     * @param bbsUserSign 会员签到
     * @return 会员签到
     */
    @Override
    public List<BbsUserSign> selectBbsUserSigninList(BbsUserSign bbsUserSign)
    {
        return bbsUserSignMapper.select(bbsUserSign);
    }

    /**
     * 新增会员签到
     * 
     * @param bbsUserSign 会员签到
     * @return 结果
     */
    @Override
    public int insertBbsUserSignin(BbsUserSign bbsUserSign)
    {
        bbsUserSign.setCreateTime(DateUtils.getNowDate());
        return bbsUserSignMapper.insertSelective(bbsUserSign);
    }

    /**
     * 修改会员签到
     * 
     * @param bbsUserSign 会员签到
     * @return 结果
     */
    @Override
    public int updateBbsUserSignin(BbsUserSign bbsUserSign)
    {
        bbsUserSign.setUpdateTime(DateUtils.getNowDate());
        return bbsUserSignMapper.updateByPrimaryKeySelective(bbsUserSign);
    }

    /**
     * 删除会员签到对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBbsUserSigninByIds(String ids)
    {
        return bbsUserSignMapper.deleteByIds(ids);
    }

    /**
     * 删除会员签到信息
     * 
     * @param userSigninId 会员签到ID
     * @return 结果
     */
    @Override
    public int deleteBbsUserSigninById(Long id)
    {
        return bbsUserSignMapper.deleteByPrimaryKey(id);
    }

    /**
     * 获取当日签到的对应数据
     * @param userId
     * @return
     */
    @Override
    public BbsSignInfo getSign(Long userId)
    {
        BbsSignInfo signInfo = new BbsSignInfo();
        BbsUserSign lastSing = bbsUserSignMapper.getUserSignInfo(userId);
        if (lastSing != null)
        {
            String today = DateUtils.dateTimeNow("yyyyMMdd");
            String lastDate = lastSing.getLast();
            // 昨天
            String yesterday = DateUtils.getYesterday("yyyyMMdd");
            if (today.compareTo(lastDate) > 0)
            {
                if (yesterday.equals(lastDate))
                {
                    signInfo.setDays(lastSing.getContinuous());
                    int reward = signRuleService.getReward(signInfo.getDays());
                    signInfo.setKiss(reward);
                }
            }
            else
            {
                // 今日已签到或其他情况，不能签到
                signInfo.setSigned(true);
                signInfo.setDays(lastSing.getContinuous());
                signInfo.setKiss(lastSing.getKissChange());
            }
        }
        return signInfo;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.dev.bbs.service.IBbsUserSigninService#sign(com.dev.bbs.domain.
     * BbsUser)
     */
    @Override
    public BbsSignInfo sign(BbsUser user)
    {
        BbsSignInfo sign = getSign(user.getUserId());
        if (!sign.isSigned())
        {
            // 添加签到记录
            // user积分
            BbsUserSign bbsUserSign = new BbsUserSign();
            bbsUserSign.setUserId(user.getUserId()).setContinuous(sign.getDays() + 1).setKissChange(sign.getKiss())
                    .setLast(DateUtils.dateTimeNow("yyyyMMdd"));
            if (insertBbsUserSignin(bbsUserSign) > 0)
            {
                userService.updateBbsUser(
                        new BbsUser().setUserId(user.getUserId()).setKiss(user.getKiss() + sign.getKiss()));
            }
            sign.setDays(sign.getDays() + 1);
            sign.setSigned(true);
        }
        return sign;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.dev.bbs.service.IBbsUserSignService#signRank()
     */
    @Override
    public List<List<RankSign>> getSignRank()
    {
        // 最快榜
        List<RankSign> fast = bbsUserSignMapper.getFastToday();
        // 最新榜
        List<RankSign> last = bbsUserSignMapper.getLast();
        // 连续榜
        List<RankSign> days = bbsUserSignMapper.getMaxDays();
        List<List<RankSign>> rank = new ArrayList<List<RankSign>>();
        rank.add(last);
        rank.add(fast);
        rank.add(days);
        return rank;
    }
}
